package singlejartest;



import com.dukascopy.api.system.*;
import com.dukascopy.api.*;
  
import java.util.HashSet;
import java.util.Set;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.io.*;
  
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
  
/**
 *
 * @author brian
 */
public class MyTesterMain {
  private static final Logger LOGGER = LoggerFactory.getLogger ( MyTesterMain.class );
  
  //url of the DEMO jnlp
  private static String   jnlpUrl   = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
  private static String userName = "DEMO2aaynx";
  //password
  private static String password = "aaynx";
  private static long     testPID   = -1;
  
  private static ITesterClient testClient;
  
  public static void main ( String[] args )
          throws Exception {
    //get the instance of the IClient interface
    testClient = com.dukascopy.api.system.TesterFactory.getDefaultInstance();
  
    //set the listener that will receive system events
    testClient.setSystemListener ( new MySystemListener ( LOGGER, testClient, userName, password ) );
  
    LOGGER.info ( "Connecting..." );
    //connect to the server using jnlp, user name and password
    testClient.connect ( jnlpUrl, userName, password );
  
    //wait for it to connect
    for ( int i = 0; i < 10 && !testClient.isConnected(); ++i ) {
      Thread.sleep ( 1000 );
    }
  
    if ( !testClient.isConnected() ) {
        LOGGER.error ( "Unable to connect" );
        System.exit ( 1 );
    }
  
    //subscribe to the instruments
    Set<Instrument> instruments = new HashSet<Instrument>();
    LOGGER.info ( "Subscribing instruments..." );
  
    instruments.add ( Instrument.GBPUSD );
    testClient.setSubscribedInstruments ( instruments );
  
    File cacheDir = new File ( "C:\\togo\\cache" );
    cacheDir.mkdir();
    testClient.setCacheDirectory ( cacheDir );
  
    Calendar fromDate = new GregorianCalendar();
    Calendar toDate   = new GregorianCalendar();
  
    fromDate.setTimeZone ( java.util.TimeZone.getTimeZone ( "UTC" ) );
    fromDate.set ( 2010,    // year
                   01,      // month
                   04,      // day
                   01,      // hour
                   00 );    // min
  
    toDate.setTimeZone ( java.util.TimeZone.getTimeZone ( "UTC" ) );
    toDate.set ( 2010,      // year
                 05,        // month
                 07,        // day
                 12,        // hour
                 00 );      // min
  
    testClient.setDataInterval (  Period.TEN_SECS,
                                  OfferSide.BID,
                                  ITesterClient.InterpolationMethod.FOUR_TICKS,
                                  fromDate.getTimeInMillis(),
                                  toDate.getTimeInMillis() );
    testClient.setInitialDeposit ( java.util.Currency.getInstance ( "USD" ), 50000 );
    testClient.setLeverage ( 100 );
    testClient.setMCEquity ( 0 );   // this is a lower threshold.  If the equity goes below
                                    // this value, positions/orders are closed
    // Logging stuff
    testClient.setProcessingStatsEnabled  ( false );
    testClient.setEventLogEnabled         ( false );
    testClient.setGatherReportData        ( false );
  
    //start the strategy
    LOGGER.info ( "Starting strategy" );
    MA_Play mystrat;
    mystrat = new MA_Play ();
    testPID = testClient.startStrategy ( mystrat );
  
    // Do we just loop here for awhile, or what?  Based on the documentation,
    // it sounds like startStrategy spawns a new process then returns immediately.
    // If main() returns, will this process exit?
  }
}